package com.briup.estore.service.impl;

import com.briup.estore.bean.Book;
import com.briup.estore.bean.BookExample;
import com.briup.estore.bean.ShopCart;
import com.briup.estore.bean.ShopCartExample;
import com.briup.estore.bean.extend.ShopCartExtend;
import com.briup.estore.dao.BookMapper;
import com.briup.estore.dao.ShopcartMapper;
import com.briup.estore.dao.extend.ShopCartExtendMapper;
import com.briup.estore.service.ShopCartService;
import com.briup.estore.util.MybatisSqlSessionFactory;
import com.briup.estore.vm.BookVM1;
import org.apache.ibatis.session.SqlSession;

import java.math.BigDecimal;
import java.util.List;

public class ShopCartServiceImpl implements ShopCartService {

    private ShopcartMapper shopcartMapper;
    private BookMapper bookMapper;
    private ShopCartExtendMapper shopCartExtendMapper;

    {
        SqlSession sqlSession = MybatisSqlSessionFactory.openSession();
        shopcartMapper = sqlSession.getMapper(ShopcartMapper.class);
        bookMapper = sqlSession.getMapper(BookMapper.class);
        shopCartExtendMapper = sqlSession.getMapper(ShopCartExtendMapper.class);
    }


    @Override
    public void addBook2ShopCart(ShopCart shopCart) {
        // 如果相同的userId购买了相同的bookId，那么做更新操作
        // 根据userId和bookId进行查询
        ShopCartExample example = new ShopCartExample();
        example.createCriteria().andUserIdEqualTo(shopCart.getUserId()).andBookIdEqualTo(shopCart.getBookId());
        // 查询
        List<ShopCart> list = shopcartMapper.selectByExample(example);
        // 根据bookId查询book对象
        Book book = bookMapper.selectByPrimaryKey(shopCart.getBookId());
        // 如果总金额为空，简单计算一下
        // 注意一下总金额可以改为double
        // 如果最后的乘积不符合要求，可以保留两位小数
        // BigDecimal
        BigDecimal b1 = new BigDecimal(book.getPrice());
        BigDecimal b2 = new BigDecimal(shopCart.getNum());
        double total = b1.multiply(b2).setScale(2).doubleValue();
        // 虽然查出来返回为list，但是按照逻辑要求，只能有一条记录
        if(list ==null || list.size()==0) {

            if (shopCart.getTotal() == null) {
                shopCart.setTotal(total);
            }
            shopcartMapper.insert(shopCart);

        }else{
            // 数据库里查出来的数据
            ShopCart sc = list.get(0);
            // 用传入的数据，加上数据库里有的数据，做更新
            ShopCart s1 = new ShopCart();
            s1.setId(sc.getId());
            s1.setNum(sc.getNum()+shopCart.getNum());
            s1.setTotal(sc.getTotal()+total);
            shopcartMapper.updateByPrimaryKeySelective(s1);

        }
    }

    @Override
    public List<ShopCart> findShopCartByUserId(Integer userId) {
        // 按条件查询，使用example
        ShopCartExample example = new ShopCartExample();
        example.createCriteria().andUserIdEqualTo(userId);

        return shopcartMapper.selectByExample(example);
    }

    @Override
    public List<ShopCartExtend> findShopCartDetailByUserId(Integer userId) {
        return shopCartExtendMapper.findAllByUserId(userId);
    }

    @Override
    public Integer removeFromShopCart(Integer userId,List<BookVM1> bookVM1List) {
        for(BookVM1 b: bookVM1List) {
            ShopCartExample example = new ShopCartExample();
            example.createCriteria().andBookIdEqualTo(b.getId()).andUserIdEqualTo(userId);
            shopcartMapper.deleteByExample(example);
        }
        return bookVM1List.size();
    }
}
